package pers.qianyu.month_202103.date_20210328;

import org.junit.Assert;
import org.junit.Test;

import java.util.HashSet;

/**
 * 5713. 字符串中不同整数的数目
 * https://leetcode-cn.com/problems/number-of-different-integers-in-a-string/
 *
 * @author mizzle rain
 * @date 2021-03-28 13点56分
 */
public class NumDifferentIntegers2 {
    public int numDifferentIntegers(String word) {
        int n = word.length();
        if (n == 0) return 0;
        char[] arr = word.toCharArray();
        HashSet<String> set = new HashSet<>();
        for (int i = 0; i < n; i++) {
            if (arr[i] < '0' || arr[i] > '9') continue;
            int start = i, end = i;
            while (end < n && arr[end] >= '0' && arr[end] <= '9') end++;
            // 如果全 0 的话，就是空字符串
            while (start < end && arr[start] == '0') start++;
            set.add(new String(arr, start, end - start));
            i = end;
        }
        return set.size();
    }

    @Test
    public void test1() {
        int r = new NumDifferentIntegers2().numDifferentIntegers("a123bc34d8ef34");
        Assert.assertEquals(3, r);
    }

    @Test
    public void test2() {
        int r = new NumDifferentIntegers2().numDifferentIntegers("leet1234code234");
        Assert.assertEquals(2, r);
    }

    @Test
    public void test3() {
        int r = new NumDifferentIntegers2().numDifferentIntegers("a1b01c001");
        Assert.assertEquals(1, r);
    }

    @Test
    public void test4() {
        int r = new NumDifferentIntegers2().numDifferentIntegers("167278959591294");
        Assert.assertEquals(1, r);
    }

    @Test
    public void test5() {
        int r = new NumDifferentIntegers2().numDifferentIntegers("0a0");
        Assert.assertEquals(1, r);
    }

    @Test
    public void test6() {
        String s1 = "000123";
        char[] chars = s1.toCharArray();
        int i = 0;
        while (chars[i] == '0') i++;
        System.out.println(new String(chars, i, chars.length - i));
    }
}
